package capstoneproject.dudiachi.Main;

import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;

import android.content.res.AssetManager;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.Bundle;
import android.util.Log;
import android.widget.GridView;
import capstoneproject.dudiachi.Controller.OldProvinceManager;
import capstoneproject.dudiachi.Model.OldProvince;

public class OldProvinceActivity extends BaseActivity {

	private GridView oldProvinceView;
	private ArrayList<Bitmap> bitArray;
	private OldProvinceGridCellAdapter oldProvinceAdapter;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		BaseActivity.POS = 0;
		setContentView(R.layout.activity_grid);
		bitArray = new ArrayList<Bitmap>();		
		OldProvinceManager opManager = new OldProvinceManager(getApplicationContext());
		for(OldProvince item : opManager.getListOldProvince()){
			try {
				bitArray.add(getBitmapFromAsset("Icons/DiaChi/" + item.getOldProvinceIcon()));
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
		
		oldProvinceView = (GridView) findViewById(R.id.grid);
		oldProvinceAdapter = new OldProvinceGridCellAdapter(getApplicationContext(), opManager.getListOldProvince(), bitArray);
		oldProvinceAdapter.notifyDataSetChanged();
		oldProvinceView.setAdapter(oldProvinceAdapter);
	}

	private Bitmap getBitmapFromAsset(String strName) throws IOException {
		AssetManager assetManager = getAssets();
		InputStream istr = assetManager.open("img/" + strName);
		Bitmap bitmap = null;
		bitmap = BitmapFactory.decodeStream(istr);
		return bitmap;
	}
}
